package com.clss.emergencycall.utils;

import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import static com.clss.emergencycall.module.imui.util.AudioRecoderUtils.CHAT_AUDIO_PATH;

/**
 * Created by Administrator on 2017/7/7.
 */

public class SdCard {
    private static final String TAG = "SdCard";

    private final static String folder = "/peoplesafety"; // 根目录
    private final static String img = "/img"; // 图片目录
    private final static String video = "/video"; // 视频目录
    private final static String audio = "/audio"; // 音频目录
    private final static String clue = "/clue"; // 线索文件目录
    private final static String download = "/download"; // 下载目录
    private final static String tmp = "/tmp"; // 临时目录
    private final static String log = "/log"; // 日志
    private final static String headfile = download + "/headfile"; //头像文件

    public static String getRootPath() {
        File file = new File(Environment.getExternalStorageDirectory().toString() + folder);
        if (!file.exists())
            file.mkdirs();

        return Environment.getExternalStorageDirectory().toString() + folder;
    }

    public static String getImg() {
        File file = new File(getRootPath() + img);
        if (!file.exists())
            file.mkdirs();

        return getRootPath() + img;
    }

    public static String getVideo() {
        File file = new File(getRootPath() + video);
        if (!file.exists())
            file.mkdirs();

        return getRootPath() + video;
    }

    public static String getAudio() {
        File file = new File(getRootPath() + audio);
        if (!file.exists())
            file.mkdirs();
        return getRootPath() + audio;
    }
    public static String getClue() {
        File file = new File(getRootPath() + clue);
        if (!file.exists())
            file.mkdirs();
        return getRootPath() + clue;
    }

    public static String getTmp() {
        File file = new File(getRootPath() + tmp);
        if (!file.exists())
            file.mkdirs();
        return getRootPath() + tmp;
    }

    public static String getDownload() {
        File file = new File(getRootPath() + download);
        if (!file.exists())
            file.mkdirs();

        return getRootPath() + download;
    }

    public static String getLog() {
        File file = new File(getRootPath() + log);
        if (!file.exists())
            file.mkdirs();

        return getRootPath() + log;
    }

    public static String getHeadFile() {
        File file = new File(getRootPath() + headfile);
        if (!file.exists())
            file.mkdirs();

        return getRootPath() + headfile;
    }

    public static void mkdirs() {
        File file = new File(getRootPath());
        if (!file.exists())
            file.mkdirs();

			/*file = new File(getImg());
			if(!file.exists())
				file.mkdirs();

			file = new File(getImg());
			if(!file.exists())
				file.mkdirs();

			file = new File(getTmp());
			if(!file.exists())
				file.mkdirs();

			file = new File(getDownload());
			if(!file.exists())
				file.mkdirs();*/
    }

    /**
     * 获取文件夹大小(递归)
     * @param file File实例
     * @return long
     */
    public static long getFolderSize(File file) {
        long size = 0;
        try {
            File[] fileList = file.listFiles();
            for (int i = 0; i < fileList.length; i++) {
                if (fileList[i].isDirectory()) {
                    size = size + getFolderSize(fileList[i]);

                } else {
                    size = size + fileList[i].length();

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }


    /**
     * 获取当前文件夹大小，不递归子文件夹
     * @param file
     * @return
     */
    public static long getCurrentFolderSize(File file) {
        long size = 0;
        try {
            File[] fileList = file.listFiles();
            for (int i = 0; i < fileList.length; i++) {
                if (fileList[i].isDirectory()) {
                    //跳过子文件夹

                } else {
                    size = size + fileList[i].length();

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }

    /**
     * 删除指定目录下文件及目录
     *
     * @param deleteThisPath
     * @param filePath
     * @return
     */
    public static boolean deleteFolderFile(String filePath, boolean deleteThisPath) {
        if (!TextUtils.isEmpty(filePath)) {
            try {
                File file = new File(filePath);
                if (file.isDirectory()) {// 处理目录
                    File files[] = file.listFiles();
                    for (int i = 0; i < files.length; i++) {
                        deleteFolderFile(files[i].getAbsolutePath(), true);
                    }
                }
                if (deleteThisPath) {
                    if (!file.isDirectory()) {// 如果是文件，删除
                        file.delete();
                    } else {// 目录
                        if (file.listFiles().length == 0) {// 目录下没有文件或者目录，删除
                            file.delete();
                        }
                    }
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    public static String getAppCacheSize(Context context){
        File file = new File(getRootPath());
        File audioFile = new File(CHAT_AUDIO_PATH);

        Lg.i(TAG, "---peoplesafety==="+getFolderSize(file));
        Lg.i(TAG, "---audioFile==="+getFolderSize(audioFile));
        long cacheSize = getFolderSize(context.getCacheDir());
        Lg.i(TAG, "---CacheDir==="+cacheSize);
        cacheSize = cacheSize - getFolderSize(new File(context.getCacheDir().getAbsolutePath()+ "/" + ACache.cacheName));
        Lg.i(TAG, "---(CacheDir-ACache)==="+cacheSize);
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            cacheSize += getFolderSize(context.getExternalCacheDir());
            Lg.i(TAG, "---(CacheDir+ExternalCacheDir)==="+cacheSize);
        }
        String size = new DecimalFormat("0.00").format((double)(getFolderSize(file) -
                //线索社情民意草稿箱的文件2.0.3
                getFolderSize(new File(SdCard.getClue())) +
                cacheSize + getFolderSize(audioFile))/ 1000000);

        return size;
    }

    public static boolean deleteCacheFile(String filePath, Context context) {
        if (!TextUtils.isEmpty(filePath)) {
            try {
                File file = new File(filePath);
                if (file.isDirectory()) {// 处理目录
                    File files[] = file.listFiles();
                    for (int i = 0; i < files.length; i++) {
                        if(!files[i].getAbsolutePath().equals(context.getCacheDir().getAbsolutePath()+ "/" + ACache.cacheName)) {
                            deleteFolderFile(files[i].getAbsolutePath(), true);
                        }
                    }
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }
}
